#pragma once
#include <string>
#include <jsoncpp/json/json.h>
#include "Log.hpp"

using namespace LogMudule;

// 接收
class Request
{
public:
    Request() = default;
    Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
    {
    }
    // 序列化
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        out_string = root.toStyledString();
        return true;
    }
    // 反序列化
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            LOG(LogLevel::ERROR) << "Failed to parse JSON: " << reader.getFormattedErrorMessages();
            return false;
        }
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        return true;
    }
    int X() const { return _x; }
    int Y() const { return _y; }
    int Oper()  const { return _oper; }
    ~Request()
    {
    }

private:
    int _x;
    int _y;
    char _oper;
};

// 应答
class Response
{
public:
    Response() :_result(0),_code(0){}
    Response(int result, int code) : _result(result), _code(code)
    {
    }
    // 序列化
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        out_string = root.toStyledString();
        // LOG(LogLevel::DEBUG)<<out_string;
        return true;
    }
    // 反序列化
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            LOG(LogLevel::ERROR) << "Failed to parse JSON: " << reader.getFormattedErrorMessages();
            return false;
        }
        _result = root["result"].asInt();
        _code = root["code"].asInt();
        return true;
    }
    int Result() { return _result; }
    int Code() { return _code; }
    void SetResult(int result){_result=result;}
    void SetCode(int code){_code=code;}
    ~Response() {}

private:
    int _result; // 结果
    int _code;   // 错误码
};




const static std::string sep = "\r\n";

// 封包
bool EnCode(std::string &message, std::string *package)
{
    if (message.size() == 0)
        return false;
    //转成17\r\nmessage\r\n的格式
    *package = std::to_string(message.size()) + sep + message + sep;
    return true;
}
// 解包
bool Decode(std::string &package, std::string *content)
{
    auto pos = package.find(sep);
    if (pos == std::string::npos)
        return false;
    std::string content_length_str = package.substr(0, pos);
    int content_length = std::stoi(content_length_str);
    int full_length = content_length_str.size() + content_length + 2 * sep.size();

    if (package.size() < full_length)
        return false;

    *content = package.substr(pos + sep.size(), content_length);

    // package erase
    package.erase(0, full_length);
    return true;
}